import { Global } from "../../Game/info/Global";
import MusicManager from "../../MusicManager";
import Scene from "../../Scene";
import { TLayout, TButton } from "../../SceneControl";

const { ccclass, property } = cc._decorator;

@ccclass
export default class MainControl extends Scene {

    @property(cc.Node)
    bg: cc.Node = null;

    @property(cc.Node)
    player: cc.Node = null;

    @property(cc.Node)
    defenseCannon: cc.Node = null;

    @property(cc.Label)
    coinLabel: cc.Label = null;

    @property(cc.Node)
    popNode: cc.Node = null;

    @property(cc.Prefab)
    exitPop: cc.Prefab = null;

    @property(cc.Prefab)
    helpPop: cc.Prefab = null;

    @property(cc.Prefab)
    roleListPop: cc.Prefab = null;

    @property(cc.Prefab)
    storePop: cc.Prefab = null;


    @property(cc.Prefab)
    chooseModePop: cc.Prefab = null;

    @property(cc.Prefab)
    startGamePop: cc.Prefab = null;

    @property([cc.AnimationClip])
    playerAnimation: cc.AnimationClip[] = [];

    currentRoleId: number = 0;

    playerWorldPosition: cc.Vec3 = null;

    allToggles: cc.Toggle[];

    protected onLoad(): void {
        this.setRole(Global.gameInfo.roleId);
        this.coinLabel.string = Global.gameInfo.coin.toString();
    }

    start() {
        //背景滚动
        this.bg.position = cc.v3(1760, 40);
        cc.tween(this.bg)
            .set({ x: 1760 })
            .to(20, { x: -640 })
            .union()
            .repeatForever()
            .start();
        this.initScene();
    }

    public initScene() {
        let layout = new TLayout();
        let layout2 = new TLayout();
        let button = cc.find("按钮", this.node);
        layout.buttonList = [button.children.map(itme => new TButton(itme))];
        layout.buttonList = [
            [new TButton(button.children[0]), new TButton(button.children[1]), new TButton(button.children[2]), new TButton(button.children[3])]
        ];
        layout2.buttonList = [
            [new TButton(button.children[4])]
        ]
        layout.site.line = 1;
        this.tscene.addLayout(layout2);
        this.tscene.addLayout(layout, { row: 1, line: 0 });
        this.tscene.site.row = 1;
        this.tscene.onBack = () => {
            MusicManager.instance.play(MusicManager.instance.btnMove);
            cc.instantiate(this.exitPop).parent = this.popNode;
        }
        this.tscene.joinController();
    }

    /**
     * 设置角色
     * @param selfToggle 
     */
    setRole(currentRoleId: number) {
        let anim = this.player.getComponent(cc.Animation);
        let ass = this.playerAnimation[currentRoleId];
        anim.addClip(ass);
        anim.play(ass.name);
    }

    update(dt) {
        //将炮口对准怪物
        let targetPosition = this.player.convertToWorldSpaceAR(cc.Vec3.ZERO);
        this.defenseCannon.children.forEach(item => {
            let selfPosition = item.convertToWorldSpaceAR(cc.Vec3.ZERO);
            let direction = cc.v2(targetPosition.x - selfPosition.x, targetPosition.y - selfPosition.y);
            let angle = cc.misc.radiansToDegrees(cc.Vec2.angle(direction, cc.v2(0, 1)));
            item.getChildByName("炮管").angle = direction.x > 0 ? 360 - angle : angle;
        })
    }

    startGame(event) {
        if (event.getCurrentTarget().name == "6人大乱斗") {
            Global.chooseMode = 0;
        } else {
            Global.chooseMode = 1;
        }
        let startGame = cc.instantiate(this.startGamePop)
        startGame.parent = this.popNode;
    }

    /**
     * 打开角色选择界面
     */
    showRoleList() {
        MusicManager.instance.play(MusicManager.instance.btnMove);
        let roleList = cc.instantiate(this.roleListPop)
        roleList.parent = this.popNode;
        cc.tween(roleList)
            .set({ position: cc.v3(380, -550) })
            .to(0.2, { y: -280 })
            .start()
    }

    showHelp() {
        MusicManager.instance.play(MusicManager.instance.btnMove);
        cc.instantiate(this.helpPop).parent = this.popNode;
    }

    /**
     * 打开商店
     */
    showStore() {
        MusicManager.instance.play(MusicManager.instance.btnMove);
        let store = cc.instantiate(this.storePop)
        store.parent = this.popNode;
        cc.tween(store)
            .set({ y: -700 })
            .to(0.2, { y: -50 })
            .start();
    }

    /**
     * 打开商店
     */
    showChooseMode() {
        MusicManager.instance.play(MusicManager.instance.btnMove);
        let store = cc.instantiate(this.storePop)
        store.parent = this.popNode;
        cc.tween(store)
            .set({ y: -700 })
            .to(0.2, { y: -50 })
            .start();
    }
}
